/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.bean_khachhang;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author thien_000
 */
@WebServlet(name = "controller_khachhang", urlPatterns = {"/controller_khachhang"})
public class controller_khachhang extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            try {
                String action = request.getParameter("btAction");
                if (action.equals("qlkh")) {
                    bean_khachhang kh = new bean_khachhang();
                    bean_khachhang[] result = kh.hienthikhachhang();
                    request.setAttribute("INFOKH", result);
                    RequestDispatcher rd = request.getRequestDispatcher("QuanLyKhachHang.jsp");
                    rd.forward(request, response);
                } else if (action.equals("Search")) {
                    String hoten = request.getParameter("txtSearch");
                    bean_khachhang kh = new bean_khachhang();
                    bean_khachhang[] result = kh.searchLikeHoTen(hoten);
                    request.setAttribute("INFOKH", result);
                    RequestDispatcher rd = request.getRequestDispatcher("QuanLyKhachHang.jsp");
                    rd.forward(request, response);
                } else if (action.equals("Thêm mới")) {
                    RequestDispatcher rd = request.getRequestDispatcher("ThemKhachHang.jsp");
                    rd.forward(request, response);
                } else if (action.equals("Thêm khách hàng")) {
                    String field_MaKH = request.getParameter("txtMaKH");
                    String field_TaiKhoan = request.getParameter("txtTaiKhoan");
                    String field_MatKhau = request.getParameter("txtMatKhau");
                    String field_HoTen = request.getParameter("txtHoTen");
                    String field_GioiTinh = request.getParameter("txtGioiTinh");
                    String field_Email = request.getParameter("txtEmail");
                    String field_SoDT = request.getParameter("txtSoDT");
                    bean_khachhang kh = new bean_khachhang();
                    String url = "controller_khachhang?btAction=qlkh";
                    boolean result = kh.insert(field_MaKH, field_TaiKhoan, field_MatKhau, field_HoTen, field_GioiTinh, field_Email, field_SoDT);
                    if (result) {
                        RequestDispatcher rd = request.getRequestDispatcher(url);
                        rd.forward(request, response);
                    } else {
                        RequestDispatcher rd = request.getRequestDispatcher("Fail_ThemKhachHang.jsp");
                        rd.forward(request, response);
                    }
                } else if (action.equals("Delete")) {
                    String taikhoan = request.getParameter("deleteTaiKhoan");
                    bean_khachhang kh = new bean_khachhang();
                    kh.setTaiKhoan(taikhoan);
                    boolean result = kh.deleteKhachHang();
                    String url = "controller_khachhang?btAction=qlkh";
                    RequestDispatcher rd = request.getRequestDispatcher(url);
                    rd.forward(request, response);
                } else if (action.equals("Update")) {
                    String taikhoan = request.getParameter("taikhoankh");
                    String matkhau = request.getParameter("matkhaukh");
                    String hoten = request.getParameter("hotenkh");
                    String gioitinh = request.getParameter("gioitinhkh");
                    String email = request.getParameter("emailkh");
                    String sodt = request.getParameter("sodtkh");
                    bean_khachhang kh = new bean_khachhang(taikhoan, matkhau, hoten, gioitinh, email, sodt);
                    boolean result = kh.updateKhachHang();
                    String url = "controller_khachhang?btAction=qlkh";
                    RequestDispatcher rd = request.getRequestDispatcher(url);
                    rd.forward(request, response);
                } else if (action.equals("Xóa tất cả")) {
                    bean_khachhang kh = new bean_khachhang();
                    boolean result = kh.deleteAll();
                    String url = "controller_khachhang?btAction=qlkh";
                    RequestDispatcher rd = request.getRequestDispatcher(url);
                    rd.forward(request, response);
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                out.close();
            }
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
